package com.family.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.family.model.system.SysRole;
import com.family.model.vo.SysRoleQueryVo;
import com.family.system.mapper.SysRoleMapper;
import com.family.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName SysRoleServiceImpl
 * @Date 2023-05-05 13:29
 * @AUTHOR family
 **/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override

    public IPage<SysRole> selectPage(Page<SysRole> sysRolePage, SysRoleQueryVo sysRoleQueryVo) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysRoleQueryVo.getRoleName()))
            queryWrapper.like(SysRole::getRoleName, sysRoleQueryVo.getRoleName());
        IPage<SysRole> rolePage = sysRoleMapper.selectPage(sysRolePage, queryWrapper);
        long current = sysRolePage.getCurrent();
        redisTemplate.opsForValue().set("roleList" + current, rolePage, 1, TimeUnit.HOURS);
        return rolePage;
    }
}
